package org.zkdn.modules.wlpt.basicinfo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zkdn.modules.wlpt.basicinfo.entity.RoadsideDeviceAll;
import org.zkdn.modules.wlpt.basicinfo.mapper.RoadsideDeviceAllMapper;
import org.zkdn.modules.wlpt.basicinfo.service.IRoadsideDeviceAllService;
import org.zkdn.modules.wlpt.basicinfo.service.IRoadsidePoleService;
import org.zkdn.modules.wlpt.basicinfo.service.ITCrossingService;
import org.zkdn.modules.wlpt.basicinfo.service.ITRoadService;
import org.zkdn.modules.wlpt.basicinfo.vo.DeviceVO;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: roadside_device_all
 * @Author: jeecg-boot
 * @Date:   2025-06-18
 * @Version: V1.0
 */
@Service
public class RoadsideDeviceAllServiceImpl extends ServiceImpl<RoadsideDeviceAllMapper, RoadsideDeviceAll> implements IRoadsideDeviceAllService {

    @Autowired
    private RoadsideDeviceAllMapper roadsideDeviceAllMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result deleteById(String id) {
        roadsideDeviceAllMapper.deleteById(id);
        return Result.OK("删除成功！");
    }

    @Override
    public Result deleteByIds(String ids) {
        roadsideDeviceAllMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }


    @Override
    public IPage<DeviceVO> queryPageList(IPage<DeviceVO> page, QueryWrapper queryWrapper) {
        return roadsideDeviceAllMapper.queryPageList(page, queryWrapper) ;
    }

    @Override
    public Result saveByValidate(RoadsideDeviceAll roadsideDeviceAll) {
        QueryWrapper<RoadsideDeviceAll> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_id", roadsideDeviceAll.getDeviceId())
                .or().eq("serial_number", roadsideDeviceAll.getSerialNumber());
        List<RoadsideDeviceAll> list = roadsideDeviceAllMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            RoadsideDeviceAll mec = list.get(0);
            if(roadsideDeviceAll.getDeviceId().equals(mec.getDeviceId())) {
                return Result.error("设备编号'" + roadsideDeviceAll.getDeviceId() + "'已存在!" );
            }else if(roadsideDeviceAll.getSerialNumber().equals(mec.getSerialNumber())) {
                return Result.error("设备序列号'" + roadsideDeviceAll.getSerialNumber() + "'已存在!" );
            }
        }
        roadsideDeviceAllMapper.insert(roadsideDeviceAll);
        return Result.OK("添加成功！");
    }

    @Override
    public Result updateByValidate(RoadsideDeviceAll roadsideDeviceAll) {
        QueryWrapper<RoadsideDeviceAll> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", roadsideDeviceAll.getId())
                .and(wrapper -> wrapper.eq("device_id", roadsideDeviceAll.getDeviceId())
                        .or().eq("serial_number", roadsideDeviceAll.getSerialNumber()));
//        queryWrapper.eq("device_id", roadsideDeviceMec.getDeviceId());
//        queryWrapper.ne("id", roadsideDeviceMec.getId());
        List<RoadsideDeviceAll> list = roadsideDeviceAllMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            RoadsideDeviceAll mec = list.get(0);
            if(roadsideDeviceAll.getDeviceId().equals(mec.getDeviceId())) {
                return Result.error("设备编号'" + roadsideDeviceAll.getDeviceId() + "'已存在!" );
            }else if(roadsideDeviceAll.getSerialNumber().equals(mec.getSerialNumber())) {
                return Result.error("设备序列号'" + roadsideDeviceAll.getSerialNumber() + "'已存在!" );
            }
        }
        roadsideDeviceAllMapper.updateById(roadsideDeviceAll);
        return Result.OK("编辑成功！");
    }
}
